﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.UI.OpenGL.Animations;

/// <summary>
/// 振型数据集合，管理多个频率的振型数据
/// </summary>
public class ModalDataSet
{
    /// <summary>
    /// 振型数据字典，键为频率
    /// </summary>
    public Dictionary<float, ModalData> Modes { get; set; } = [];

    /// <summary>
    /// 当前选中的振型
    /// </summary>
    public ModalData? CurrentMode { get; set; }

    /// <summary>
    /// 添加振型数据
    /// </summary>
    /// <param name="modalData">振型数据</param>
    public void AddMode(ModalData modalData)
    {
        Modes[modalData.Frequency] = modalData;
        CurrentMode ??= modalData;
    }

    /// <summary>
    /// 选择振型
    /// </summary>
    /// <param name="frequency">频率</param>
    /// <returns>是否成功选择</returns>
    public bool SelectMode(float frequency)
    {
        if (Modes.TryGetValue(frequency, out var mode))
        {
            CurrentMode = mode;
            return true;
        }
        return false;
    }

    /// <summary>
    /// 获取所有频率列表
    /// </summary>
    /// <returns>频率列表</returns>
    public List<float> GetFrequencies()
    {
        var frequencies = new List<float>(Modes.Keys);
        frequencies.Sort();
        return frequencies;
    }

    /// <summary>
    /// 获取振型数量
    /// </summary>
    /// <returns>振型数量</returns>
    public int GetModeCount()
    {
        return Modes.Count;
    }

    /// <summary>
    /// 根据索引设置当前振型
    /// </summary>
    /// <param name="index">振型索引</param>
    public void SetCurrentModeIndex(int index)
    {
        var frequencies = GetFrequencies();
        if (index >= 0 && index < frequencies.Count)
        {
            SelectMode(frequencies[index]);
        }
    }
}